package org.pay.shop.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.pay.core.common.util.MySeq;
import org.pay.core.common.util.PayDigestUtil;
import org.pay.core.common.util.PayUtil;


import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by yangshancheng on 16/5/5.
 */
public class PayOrderThreadDemo {

    // 商户ID
    static final String mchId = "20000001";//20001223,20001245
    static final String appId = "3ed9f471af2148c9a6889f1ab76edd1d";
    static final String passageId = "1000";
    // 加签key
    static final String reqKey = "TYnGynNHewOWxaYjtcIEvN9MCCcrx65oGubikAXI96sVlJw9";
    // 验签key
    static final String repKey = "QCbloPFHvqsd7m6kqQy9DRPALsanQpvtoAILbxMWwc6HFyVp";

    static final String baseUrl = "https://pay.b.pay.org/api";
    //static final String baseUrl = "http://localhost:3020/api";
    //static final String notifyUrl = "http://www.baidu.com"; // 本地环境测试,可到ngrok.cc网站注册
    static final String notifyUrl = "http://shop.pay.org/goods/payNotify";

    public static void main(String[] args) {
        PayOrderThreadDemo payOrderThreadDemo = new PayOrderThreadDemo();
        payOrderThreadDemo.excutor();

    }

    public void excutor() {
        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
        int num = 0;
        for (int i = 0; i < 1000; i++) {
            num++;
            final int finalNum = num;
            singleThreadExecutor.execute(new Runnable() {
                public void run() {
                    try {
                        long startTime = System.currentTimeMillis();
                        String orderNo = MySeq.getTrans();
                        boolean result = payOrderTest(orderNo);
                        long endTime = System.currentTimeMillis();
                        System.out.println(finalNum + "[" + orderNo + "]" + result + "," + (endTime - startTime) + " ms");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            });
        }
    }

    // 统一下单
    static Boolean payOrderTest(String orderNo) {
        JSONObject paramMap = new JSONObject();
        paramMap.put("mchId", mchId);                               // 商户ID
        paramMap.put("appId", appId);
        paramMap.put("passageId", passageId);                       // 支付通道,平台账户需要传递
        paramMap.put("mchOrderNo", orderNo);     // 商户订单号
        // 支付渠道ID, WX_NATIVE(微信扫码),WX_JSAPI(微信公众号或微信小程序),WX_APP(微信APP),WX_MWEB(微信H5),ALIPAY_WAP(支付宝手机支付),ALIPAY_PC(支付宝网站支付),ALIPAY_MOBILE(支付宝移动支付)
        paramMap.put("channelId", "alipay_wap");
        paramMap.put("amount", 1);                                  // 支付金额,单位分
        paramMap.put("currency", "cny");                            // 币种, cny-人民币
        paramMap.put("clientIp", "211.94.116.218");                 // 用户地址,微信H5支付时要真实的
        paramMap.put("device", "WEB");                              // 设备
        paramMap.put("subject", "PAY支付测试");
        paramMap.put("body", "PAY支付测试");
        paramMap.put("notifyUrl", notifyUrl);                       // 回调URL
        paramMap.put("param1", "");                                 // 扩展参数1
        paramMap.put("param2", "");                                 // 扩展参数2
        paramMap.put("extra", "{\n" +
                "  \"productId\": \"120989823\",\n" +
                "  \"openId\": \"oIkQuwhPgPUgl-TvQ48_UUpZUwMs\",\n" +
                "  \"sceneInfo\": {\n" +
                "    \"h5_info\": {\n" +
                "      \"type\": \"Wap\",\n" +
                "      \"wap_url\": \"http://shop.pay.org\",\n" +
                "      \"wap_name\": \"pay充值\"\n" +
                "    }\n" +
                "  }\n" +
                " ,\"discountable_amount\":\"0.00\"," + //面对面支付扫码参数：可打折金额 可打折金额+不可打折金额=总金额
                "  \"undiscountable_amount\":\"0.00\"," + //面对面支付扫码参数：不可打折金额
                "}");  // 附加参数

        //{"h5_info": {"type":"Wap","wap_url": "https://pay.qq.com","wap_name": "腾讯充值"}}

        String reqSign = PayDigestUtil.getSign(paramMap, reqKey);
        paramMap.put("sign", reqSign);                              // 签名
        String reqData = "params=" + paramMap.toJSONString();
        //System.out.println("请求支付中心下单接口,请求数据:" + reqData);
        String url = baseUrl + "/pay/create_order?";
        String result = PayUtil.call4Post(url + reqData);
        //System.out.println("请求支付中心下单接口,响应数据:" + result);
        Map retMap = JSON.parseObject(result);
        if("SUCCESS".equals(retMap.get("retCode"))/* && "SUCCESS".equalsIgnoreCase(retMap.get("resCode").toString())*/) {
            // 验签
            String checkSign = PayDigestUtil.getSign(retMap, repKey, "sign", "payParams");
            String retSign = (String) retMap.get("sign");
            if(checkSign.equals(retSign)) {
                //System.out.println("=========支付中心下单验签成功=========");
                return true;
            }else {
                System.err.println("=========支付中心下单验签失败=========");
                //return false;
            }
        }
        return false;
    }


}
